package com.xmkj.bearuser.home;

import android.annotation.SuppressLint;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.common.adapter.helper.IRecyclerViewHelper;
import com.common.cons.Constans;
import com.common.mvp.BaseMvpFragment;
import com.common.mvp.BasePresenter;
import com.common.retrofit.bean.BearSearchBean;
import com.common.retrofit.bearusermethod.GetSearchListMethods;
import com.common.retrofit.subscriber.CommonSubscriber;
import com.common.retrofit.subscriber.SubscriberListener;
import com.common.rxbus.RxBus;
import com.common.rxbus.postevent.RxKeyEvent;
import com.common.utils.EmptyUtils;
import com.common.utils.SPUtils;
import com.common.utils.SizeUtils;
import com.common.widget.DividerDecoration.MarginDecoration;
import com.common.widget.recyclerview.refresh.recycleview.XRecyclerView;
import com.xmkj.bearuser.R;
import com.xmkj.bearuser.home.adapter.BearSearhAdapter;

import java.util.ArrayList;
import java.util.List;

import rx.functions.Action1;

/**
 * Create by leo
 * 创建日期：2018/7/27
 * 描述：
 */

@SuppressLint("ValidFragment")
public class SearchFragment extends BaseMvpFragment {
    XRecyclerView recyclerview;
    private String search;
    private  int  sort_type;
    private List<BearSearchBean> homeReccommedBeans = new ArrayList<>();
    private BearSearhAdapter homeReccommendListAdapter;

    @SuppressLint("ValidFragment")
    public SearchFragment(int position) {
        sort_type = position ;
    }

    @Override
    protected BasePresenter createPresenterInstance() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_base_reclerview;
    }

    @Override
    protected void onViewCreated(View view) {
        recyclerview = view.findViewById(R.id.recyclerview);
    }

    @Override
    protected void doLogicFunc() {
        setRecyclerView();
        rxManager.add(RxBus.getDefault().toObservable(RxKeyEvent.class).subscribe(new Action1<RxKeyEvent>() {
            @Override
            public void call(RxKeyEvent rxKeyEvent) {
                if (EmptyUtils.isNotEventEmpty(rxKeyEvent, RxKeyEvent.SEARCH_KEY) ) {
                    search = (String) rxKeyEvent.getValue();
                    mPageIndex = 1 ;
                    gotoHttp();

                }
            }
        }));
    }

    private void gotoHttp() {
        CommonSubscriber<ArrayList<BearSearchBean>> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                dismissProgressDialog();
                ArrayList<BearSearchBean> beans = (ArrayList<BearSearchBean>) o;
                List<BearSearchBean> beanList = beans;
                recyclerview.loadMoreComplete();
                // 下拉刷新
                if (mIsRefreshOrLoadMore == 0) {
                    recyclerview.refreshComplete();
                    homeReccommendListAdapter.clear();
                }

                if (EmptyUtils.isNotEmpty(beanList)) {
                    homeReccommedBeans = beanList;
                    homeReccommendListAdapter.addAll(homeReccommedBeans);
                } else if (mIsRefreshOrLoadMore == 0) {
//                    statusEmpty();
                    return;
                }

                if (EmptyUtils.isEmpty(beanList)) {
                    recyclerview.setNoMore(true);
                } else {
                    mIsHasNoData = beanList.size() < mPageSize;
                    recyclerview.setNoMore(mIsHasNoData);
                }
                homeReccommendListAdapter.notifyDataSetChanged();
                recyclerview.refreshComplete();
                recyclerview.loadMoreComplete();
                if (mIsRefreshOrLoadMore == 0) {
                    dismissProgressDialog();
                }
            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                homeReccommendListAdapter.clear();
                homeReccommendListAdapter.notifyDataSetChanged();
                showToastMsg(e);
                recyclerview.refreshComplete();
                recyclerview.loadMoreComplete();
                if (mIsRefreshOrLoadMore == 0) {
                    dismissProgressDialog();
                }
            }
        });


        GetSearchListMethods.getInstance().goodsSearch(subscriber, SPUtils.getShareInt(Constans.UID), SPUtils.getShareString(Constans.HASHID),search,sort_type, mPageIndex);
        rxManager.add(subscriber);
    }
    private void setRecyclerView() {
        IRecyclerViewHelper.init().setRecycleLineLayout(context, LinearLayoutManager.VERTICAL, recyclerview);
        recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.VERTICAL, (int) SizeUtils.dp2px(context, 5)));
//        recyclerview.addItemDecoration(new DividerGridItemDecoration(context));
      /*  recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.VERTICAL, (int) SizeUtils.dp2px(context, 1)));
        recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.HORIZONTAL, (int) SizeUtils.dp2px(context, 1)));*/
//        recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.VERTICAL, (int) SizeUtils.dp2px(context, 1)));
        recyclerview.setHasFixedSize(true);
        recyclerview.setRefreshing(true);
        recyclerview.setLoadMoreEnabled(true);
        homeReccommendListAdapter = new BearSearhAdapter(context, homeReccommedBeans);
//        adapter.setOnItemClickSeclect(this);
        recyclerview.setAdapter(homeReccommendListAdapter);

        recyclerview.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mPageIndex = 1;
                mIsRefreshOrLoadMore = 0;
                gotoHttp();
            }

            @Override
            public void onLoadMore() {
                if (mIsHasNoData) {
                    recyclerview.loadMoreComplete();
                    recyclerview.setNoMore(true);
                    return;
                }
                mPageIndex++;
                mIsRefreshOrLoadMore = 1;
                gotoHttp();
            }
        });

    }
    @Override
    protected void setStatusBar() {
        super.setStatusBar();
        m_statusBar.setVisibility(View.GONE);
    }
}
